# -*- coding: utf-8 -*-
import sys
import time
sys.path.append('../..')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests
import json
import execjs
from common.helper import Helper

class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "平安一年女性特定疾病保险"
    # 生成产品数据
    def genProductInfo( self ):
        plan = {
            "year":[str(i) for i in range(18,61)],
            "gender":["M"],
            "payType":["年交"],
            "expire":["1年"],
            "specialWomenDisease":["100000"]
        }
        
        for v in product( plan["year"],plan["gender"],plan["payType"],plan["expire"],plan["specialWomenDisease"]):
            data = Helper.formatProductInfo(plan,v )
            self.mysql.cursor.execute( domain[self.name]["insert_sql"].format(**data))
            self.mysql._conn.commit()
            print(domain[self.name]["insert_sql"].format(**data) )
    # 执行爬虫
    def requestJson(self,dt=None,mysql=None):
        payload = self.genPayloadParams(dt)
        r = requests.post( url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload)
        if r.status_code == 200 and "premium" in r.text:
            price = Helper.decimalParse(r.json()["DATA"]["mainProductAmount"])
            data = {
                "name":self.name,
                "id":dt["id"],
                "price":price
            }
            print( data )
            mysql.updatePrice( data )
        else:
            data = {
                "name":self.name,
                "id":dt["id"],
                "state":3
            }
            mysql.updateTask( data )
            print("\n/*产品价格信息更新失败:%d/"%dt["id"])



    # 生成签名
    def genPayloadParams(self,dt):
        js = '''
        //定义n函数
        function n(r) {
                var e = r.length,
                    a = new Array(64);
                if (e < 4) {
                    var n = 0,
                        s = 0,
                        f = 0,
                        o = 0;
                    for (n = 0; n < e; n++) {
                        var c = r.charCodeAt(n);
                        for (s = 0; s < 16; s++) {
                            var t = 1,
                                l = 0;
                            for (l = 15; l > s; l--)
                                t *= 2;
                            a[16 * n + s] = parseInt(c / t) % 2
                        }
                    }
                    for (f = e; f < 4; f++) {
                        c = 0;
                        for (o = 0; o < 16; o++) {
                            t = 1,
                                l = 0;
                            for (l = 15; l > o; l--)
                                t *= 2;
                            a[16 * f + o] = parseInt(c / t) % 2
                        }
                    }
                } else
                    for (n = 0; n < 4; n++) {
                        c = r.charCodeAt(n);
                        for (s = 0; s < 16; s++) {
                            t = 1;
                            for (l = 15; l > s; l--)
                                t *= 2;
                            a[16 * n + s] = parseInt(c / t) % 2
                        }
                    }
                return a
            }
            //定义a函数

        function a(r) {
                var e = new Array,
                    a = r.length,
                    s = parseInt(a / 4),
                    f = a % 4,
                    o = 0;
                for (o = 0; o < s; o++)
                    e[o] = n(r.substring(4 * o + 0, 4 * o + 4));
                return f > 0 && (e[o] = n(r.substring(4 * o + 0, a))),
                    e
            }
            //定义g函数

        function g(r) {
                var e = new Array(56),
                    a = new Array;
                a[0] = new Array,
                    a[1] = new Array,
                    a[2] = new Array,
                    a[3] = new Array,
                    a[4] = new Array,
                    a[5] = new Array,
                    a[6] = new Array,
                    a[7] = new Array,
                    a[8] = new Array,
                    a[9] = new Array,
                    a[10] = new Array,
                    a[11] = new Array,
                    a[12] = new Array,
                    a[13] = new Array,
                    a[14] = new Array,
                    a[15] = new Array;
                for (var n = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1], s = 0; s < 7; s++)
                    for (var f = 0, o = 7; f < 8; f++,
                        o--)
                        e[8 * s + f] = r[8 * o + s];
                s = 0;
                for (s = 0; s < 16; s++) {
                    var c = 0,
                        t = 0;
                    for (f = 0; f < n[s]; f++) {
                        for (c = e[0],
                            t = e[28],
                            o = 0; o < 27; o++)
                            e[o] = e[o + 1],
                            e[28 + o] = e[29 + o];
                        e[27] = c,
                            e[55] = t
                    }
                    var l = new Array(48);
                    switch (l[0] = e[13],
                        l[1] = e[16],
                        l[2] = e[10],
                        l[3] = e[23],
                        l[4] = e[0],
                        l[5] = e[4],
                        l[6] = e[2],
                        l[7] = e[27],
                        l[8] = e[14],
                        l[9] = e[5],
                        l[10] = e[20],
                        l[11] = e[9],
                        l[12] = e[22],
                        l[13] = e[18],
                        l[14] = e[11],
                        l[15] = e[3],
                        l[16] = e[25],
                        l[17] = e[7],
                        l[18] = e[15],
                        l[19] = e[6],
                        l[20] = e[26],
                        l[21] = e[19],
                        l[22] = e[12],
                        l[23] = e[1],
                        l[24] = e[40],
                        l[25] = e[51],
                        l[26] = e[30],
                        l[27] = e[36],
                        l[28] = e[46],
                        l[29] = e[54],
                        l[30] = e[29],
                        l[31] = e[39],
                        l[32] = e[50],
                        l[33] = e[44],
                        l[34] = e[32],
                        l[35] = e[47],
                        l[36] = e[43],
                        l[37] = e[48],
                        l[38] = e[38],
                        l[39] = e[55],
                        l[40] = e[33],
                        l[41] = e[52],
                        l[42] = e[45],
                        l[43] = e[41],
                        l[44] = e[49],
                        l[45] = e[35],
                        l[46] = e[28],
                        l[47] = e[31],
                        s) {
                    case 0:
                        for (var u = 0; u < 48; u++)
                            a[0][u] = l[u];
                        break;
                    case 1:
                        for (u = 0; u < 48; u++)
                            a[1][u] = l[u];
                        break;
                    case 2:
                        for (u = 0; u < 48; u++)
                            a[2][u] = l[u];
                        break;
                    case 3:
                        for (u = 0; u < 48; u++)
                            a[3][u] = l[u];
                        break;
                    case 4:
                        for (u = 0; u < 48; u++)
                            a[4][u] = l[u];
                        break;
                    case 5:
                        for (u = 0; u < 48; u++)
                            a[5][u] = l[u];
                        break;
                    case 6:
                        for (u = 0; u < 48; u++)
                            a[6][u] = l[u];
                        break;
                    case 7:
                        for (u = 0; u < 48; u++)
                            a[7][u] = l[u];
                        break;
                    case 8:
                        for (u = 0; u < 48; u++)
                            a[8][u] = l[u];
                        break;
                    case 9:
                        for (u = 0; u < 48; u++)
                            a[9][u] = l[u];
                        break;
                    case 10:
                        for (u = 0; u < 48; u++)
                            a[10][u] = l[u];
                        break;
                    case 11:
                        for (u = 0; u < 48; u++)
                            a[11][u] = l[u];
                        break;
                    case 12:
                        for (u = 0; u < 48; u++)
                            a[12][u] = l[u];
                        break;
                    case 13:
                        for (u = 0; u < 48; u++)
                            a[13][u] = l[u];
                        break;
                    case 14:
                        for (u = 0; u < 48; u++)
                            a[14][u] = l[u];
                        break;
                    case 15:
                        for (u = 0; u < 48; u++)
                            a[15][u] = l[u]
                    }
                }
                return a
            }
            //定义b函数

        function b(r) {
                for (var e = new Array(64), a = 0, n = 1, s = 0; a < 4; a++,
                    n += 2,
                    s += 2)
                    for (var f = 7, o = 0; f >= 0; f--,
                        o++)
                        e[8 * a + o] = r[8 * f + n],
                        e[8 * a + o + 32] = r[8 * f + s];
                return e
            }
            //定义i函数

        function i(r, e) {
                for (var a = new Array(r.length), n = 0; n < r.length; n++)
                    a[n] = r[n] ^ e[n];
                return a
            }
            //定义w函数

        function w(r) {
                var e = new Array(32);
                return e[0] = r[15],
                    e[1] = r[6],
                    e[2] = r[19],
                    e[3] = r[20],
                    e[4] = r[28],
                    e[5] = r[11],
                    e[6] = r[27],
                    e[7] = r[16],
                    e[8] = r[0],
                    e[9] = r[14],
                    e[10] = r[22],
                    e[11] = r[25],
                    e[12] = r[4],
                    e[13] = r[17],
                    e[14] = r[30],
                    e[15] = r[9],
                    e[16] = r[1],
                    e[17] = r[7],
                    e[18] = r[23],
                    e[19] = r[13],
                    e[20] = r[31],
                    e[21] = r[26],
                    e[22] = r[2],
                    e[23] = r[8],
                    e[24] = r[18],
                    e[25] = r[12],
                    e[26] = r[29],
                    e[27] = r[5],
                    e[28] = r[21],
                    e[29] = r[10],
                    e[30] = r[3],
                    e[31] = r[24],
                    e
            }
            //定义y函数

        function y(r) {
                var e = "";
                switch (r) {
                case 0:
                    e = "0000";
                    break;
                case 1:
                    e = "0001";
                    break;
                case 2:
                    e = "0010";
                    break;
                case 3:
                    e = "0011";
                    break;
                case 4:
                    e = "0100";
                    break;
                case 5:
                    e = "0101";
                    break;
                case 6:
                    e = "0110";
                    break;
                case 7:
                    e = "0111";
                    break;
                case 8:
                    e = "1000";
                    break;
                case 9:
                    e = "1001";
                    break;
                case 10:
                    e = "1010";
                    break;
                case 11:
                    e = "1011";
                    break;
                case 12:
                    e = "1100";
                    break;
                case 13:
                    e = "1101";
                    break;
                case 14:
                    e = "1110";
                    break;
                case 15:
                    e = "1111"
                }
                return e
            }
            //定义v函数

        function v(r) {
                for (var e = new Array(32), a = "", n = [
                    [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
                    [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
                    [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
                    [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]
                ], s = [
                    [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
                    [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
                    [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
                    [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]
                ], f = [
                    [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
                    [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
                    [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
                    [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]
                ], o = [
                    [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
                    [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
                    [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
                    [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]
                ], c = [
                    [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
                    [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
                    [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
                    [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]
                ], t = [
                    [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
                    [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
                    [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
                    [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]
                ], l = [
                    [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
                    [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
                    [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
                    [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]
                ], u = [
                    [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
                    [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
                    [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
                    [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]
                ], b = 0; b < 8; b++) {
                    var k, i;
                    switch (k = 2 * r[6 * b + 0] + r[6 * b + 5],
                        i = 2 * r[6 * b + 1] * 2 * 2 + 2 * r[6 * b + 2] * 2 + 2 * r[6 * b + 3] + r[6 * b + 4],
                        b) {
                    case 0:
                        a = y(n[k][i]);
                        break;
                    case 1:
                        a = y(s[k][i]);
                        break;
                    case 2:
                        a = y(f[k][i]);
                        break;
                    case 3:
                        a = y(o[k][i]);
                        break;
                    case 4:
                        a = y(c[k][i]);
                        break;
                    case 5:
                        a = y(t[k][i]);
                        break;
                    case 6:
                        a = y(l[k][i]);
                        break;
                    case 7:
                        a = y(u[k][i])
                    }
                    e[4 * b + 0] = parseInt(a.substring(0, 1)),
                        e[4 * b + 1] = parseInt(a.substring(1, 2)),
                        e[4 * b + 2] = parseInt(a.substring(2, 3)),
                        e[4 * b + 3] = parseInt(a.substring(3, 4))
                }
                return e
            }
            //定义A函数

        function A(r) {
                var e = new Array(64);
                return e[0] = r[39],
                    e[1] = r[7],
                    e[2] = r[47],
                    e[3] = r[15],
                    e[4] = r[55],
                    e[5] = r[23],
                    e[6] = r[63],
                    e[7] = r[31],
                    e[8] = r[38],
                    e[9] = r[6],
                    e[10] = r[46],
                    e[11] = r[14],
                    e[12] = r[54],
                    e[13] = r[22],
                    e[14] = r[62],
                    e[15] = r[30],
                    e[16] = r[37],
                    e[17] = r[5],
                    e[18] = r[45],
                    e[19] = r[13],
                    e[20] = r[53],
                    e[21] = r[21],
                    e[22] = r[61],
                    e[23] = r[29],
                    e[24] = r[36],
                    e[25] = r[4],
                    e[26] = r[44],
                    e[27] = r[12],
                    e[28] = r[52],
                    e[29] = r[20],
                    e[30] = r[60],
                    e[31] = r[28],
                    e[32] = r[35],
                    e[33] = r[3],
                    e[34] = r[43],
                    e[35] = r[11],
                    e[36] = r[51],
                    e[37] = r[19],
                    e[38] = r[59],
                    e[39] = r[27],
                    e[40] = r[34],
                    e[41] = r[2],
                    e[42] = r[42],
                    e[43] = r[10],
                    e[44] = r[50],
                    e[45] = r[18],
                    e[46] = r[58],
                    e[47] = r[26],
                    e[48] = r[33],
                    e[49] = r[1],
                    e[50] = r[41],
                    e[51] = r[9],
                    e[52] = r[49],
                    e[53] = r[17],
                    e[54] = r[57],
                    e[55] = r[25],
                    e[56] = r[32],
                    e[57] = r[0],
                    e[58] = r[40],
                    e[59] = r[8],
                    e[60] = r[48],
                    e[61] = r[16],
                    e[62] = r[56],
                    e[63] = r[24],
                    e
            }
            //定义l函数

        function l(r, e) {
                var a = g(e),
                    n = b(r),
                    s = new Array(32),
                    f = new Array(32),
                    o = new Array(32),
                    c = 0,
                    t = 0,
                    l = 0,
                    u = 0,
                    y = 0;
                for (l = 0; l < 32; l++)
                    s[l] = n[l],
                    f[l] = n[32 + l];
                for (c = 0; c < 16; c++) {
                    for (t = 0; t < 32; t++)
                        o[t] = s[t],
                        s[t] = f[t];
                    var h = new Array(48);
                    for (u = 0; u < 48; u++)
                        h[u] = a[c][u];
                    var p = i(w(v(i(k(f), h))), o);
                    for (y = 0; y < 32; y++)
                        f[y] = p[y]
                }
                var I = new Array(64);
                for (c = 0; c < 32; c++)
                    I[c] = f[c],
                    I[32 + c] = s[c];
                return A(I)
            }
            //定义k函数

        function k(r) {
                for (var e = new Array(48), a = 0; a < 8; a++)
                    e[6 * a + 0] = 0 == a ? r[31] : r[4 * a - 1],
                    e[6 * a + 1] = r[4 * a + 0],
                    e[6 * a + 2] = r[4 * a + 1],
                    e[6 * a + 3] = r[4 * a + 2],
                    e[6 * a + 4] = r[4 * a + 3],
                    e[6 * a + 5] = 7 == a ? r[0] : r[4 * a + 4];
                return e
            }
            //定义s函数

        function s(r) {
                var e;
                switch (r) {
                case "0000":
                    e = "0";
                    break;
                case "0001":
                    e = "1";
                    break;
                case "0010":
                    e = "2";
                    break;
                case "0011":
                    e = "3";
                    break;
                case "0100":
                    e = "4";
                    break;
                case "0101":
                    e = "5";
                    break;
                case "0110":
                    e = "6";
                    break;
                case "0111":
                    e = "7";
                    break;
                case "1000":
                    e = "8";
                    break;
                case "1001":
                    e = "9";
                    break;
                case "1010":
                    e = "A";
                    break;
                case "1011":
                    e = "B";
                    break;
                case "1100":
                    e = "C";
                    break;
                case "1101":
                    e = "D";
                    break;
                case "1110":
                    e = "E";
                    break;
                case "1111":
                    e = "F"
                }
                return e
            }
            //定义c函数

        function c(r) {
                for (var e = "", a = 0; a < 16; a++) {
                    for (var n = "", f = 0; f < 4; f++)
                        n += r[4 * a + f];
                    e += s(n)
                }
                return e
            }
            // 生成签名函数	

        function genPayloadParam(r) {
            return null == r || "" == r ? "" : function (r, e, s, f) {
                var o, t, u, b, k, i, v = r.length,
                    w = "";
                if (null != e && "" != e && (b = (o = a(e)).length),
                    null != s && "" != s && (k = (t = a(s)).length),
                    null != f && "" != f && (i = (u = a(f)).length),
                    v > 0)
                    if (v < 4) {
                        var A = n(r);
                        if (null != e && "" != e && null != s && "" != s && null != f && "" != f) {
                            for (V = A,
                                y = 0; y < b; y++)
                                V = l(V, o[y]);
                            for (S = 0; S < k; S++)
                                V = l(V, t[S]);
                            for (C = 0; C < i; C++)
                                V = l(V, u[C]);
                            d = V
                        } else if (null != e && "" != e && null != s && "" != s) {
                            for (V = A,
                                y = 0; y < b; y++)
                                V = l(V, o[y]);
                            for (S = 0; S < k; S++)
                                V = l(V, t[S]);
                            d = V
                        } else if (null != e && "" != e) {
                            var y = 0;
                            for (V = A,
                                y = 0; y < b; y++)
                                V = l(V, o[y]);
                            d = V
                        }
                        w = c(d)
                    } else {
                        var g = parseInt(v / 4),
                            h = v % 4,
                            p = 0;
                        for (p = 0; p < g; p++) {
                            var I = n(r.substring(4 * p + 0, 4 * p + 4));
                            if (null != e && "" != e && null != s && "" != s && null != f && "" != f) {
                                for (V = I,
                                    y = 0; y < b; y++)
                                    V = l(V, o[y]);
                                for (S = 0; S < k; S++)
                                    V = l(V, t[S]);
                                for (C = 0; C < i; C++)
                                    V = l(V, u[C]);
                                d = V
                            } else if (null != e && "" != e && null != s && "" != s) {
                                for (V = I,
                                    y = 0; y < b; y++)
                                    V = l(V, o[y]);
                                for (S = 0; S < k; S++)
                                    V = l(V, t[S]);
                                d = V
                            } else if (null != e && "" != e) {
                                for (V = I,
                                    y = 0; y < b; y++)
                                    V = l(V, o[y]);
                                d = V
                            }
                            w += c(d)
                        }
                        if (h > 0) {
                            var d;
                            I = n(r.substring(4 * g + 0, v));
                            if (null != e && "" != e && null != s && "" != s && null != f && "" != f) {
                                var C;
                                for (V = I,
                                    y = 0; y < b; y++)
                                    V = l(V, o[y]);
                                for (S = 0; S < k; S++)
                                    V = l(V, t[S]);
                                for (C = 0; C < i; C++)
                                    V = l(V, u[C]);
                                d = V
                            } else if (null != e && "" != e && null != s && "" != s) {
                                var S;
                                for (V = I,
                                    y = 0; y < b; y++)
                                    V = l(V, o[y]);
                                for (S = 0; S < k; S++)
                                    V = l(V, t[S]);
                                d = V
                            } else if (null != e && "" != e) {
                                var V;
                                for (V = I,
                                    y = 0; y < b; y++)
                                    V = l(V, o[y]);
                                d = V
                            }
                            w += c(d)
                        }
                    }
                return w
            }(r, "7XeW+OCUl7Y=", "&WQvClhM3Vc=", "aDiSwUAluOc=")
        }
        ''' 
        cxt = execjs.compile( js )
        r =  '{"productCode":"1062","masterInfo":{"masterKeys":[{"key":"1062|FLOW_INS_AGE_GROUP","value":"'+str(2020-int(dt["year"]))+'-01-01","unit":"Y"},{"key":"1062|FLOW_SUM_INS","value":"10","unit":"WY"},{"key":"1062|FLOW_PREIOD","value":"1","unit":"Y"},{"key":"1062|FLOW_PREM_TERM","value":"1","unit":"Y"},{"key":"1062|FLOW_INS_SEX","value":"F","unit":null}]},"appendInfo":{"appendProductList":[],"slavePlanList":[]}}'
        payload = cxt.call("genPayloadParam",r);
        return payload; 

if __name__ == "__main__":
    s = Spider()
    Helper.runProcess(mysql=s.mysql,name=s.name,func=s.requestJson)
            